package com.atguigu.day01;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

//从word.txt中读取数据，计算word count
public class Example2 {
    // 注意抛出异常
    public static void main(String[] args) throws Exception {
        //获取执行上下文环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //设置并行任务的数量为1
        env.setParallelism(1);

        //读取数据源 resources中文件全路径
        DataStreamSource<String> source = env.readTextFile("D:\\code\\git\\yanzl_PC\\flinktutorial0701\\src\\main\\resources\\word.txt");

        //使用flatMap算子
        //语义：将列表或者流中的每一个元素，转换成0个、1个、或者多个元素
        SingleOutputStreamOperator<Tuple2<String, Integer>> mappedStream =
                //匿名类
                //第一个泛型是：输入的泛型 String
                //第二个泛型是：输出的泛型 Tuple2
                source.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String in, Collector<Tuple2<String, Integer>> out) throws Exception {
                //使用空格切分字符串
                String[] arr = in.split(" ");
                //将要发送的数据收集到集合中
                for (String word : arr) {
                    out.collect(
                            Tuple2.of(word, 1)
                    );
                }
                }

        });


        //shuffle
        //将不同单词的元组shuffle到不同的逻辑分区
        //第一个泛型：输入数据的泛型 Tuple2
        //第二个泛型：输出数据的泛型 String
        KeyedStream<Tuple2<String, Integer>, String> keyedStream = mappedStream.keyBy(new KeySelector<Tuple2<String, Integer>, String>() {
            @Override
            public String getKey(Tuple2<String, Integer> value) throws Exception {
                //f0是元组的第0个元素
                //为数据指定key
                return value.f0;
            }
        });

        //reduce
        //将相同逻辑分区的数据进行聚合
        //sum是有状态的算子
        SingleOutputStreamOperator<Tuple2<String, Integer>> reducedStream = keyedStream.sum("f1");


        //打印
        reducedStream.print();




        //提交并执行程序
        env.execute();

    }

}
